/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony.remote.rpc;

import java.io.IOException;

import com.argot.TypeException;
import com.argot.TypeInputStream;
import com.argot.TypeMap;
import com.argot.TypeOutputStream;
import com.argot.TypeReader;
import com.argot.TypeWriter;

import com.argot.network.TypeEndPoint;
import com.argot.network.TypeTransport;

public class RpcMetaClient
{
	private TypeTransport _back;
	private TypeMap _refMap;
	private TypeWriter _rpcMetaRequest;
	private TypeReader _rpcMetaResponse;
	
	public RpcMetaClient( TypeTransport back, TypeMap map )
	throws TypeException
	{
		_back = back;
		_refMap = map;
		_rpcMetaRequest = map.getWriter(map.getId(RpcMetaRequest.TYPENAME));
		_rpcMetaResponse = map.getReader(map.getId(RpcMetaResponse.TYPENAME));
	}

	public void process( RpcMetaMethodRequest methodRequest ) 
	throws IOException
	{
		// Write out request to a stream.	
		TypeEndPoint endPoint = _back.openLink();
				
		try
		{
			TypeOutputStream tmos = new TypeOutputStream( endPoint.getOutputStream(), _refMap );
			_rpcMetaRequest.write(tmos, methodRequest);
			tmos.getStream().flush();
		
			TypeInputStream tmis = new TypeInputStream( endPoint.getInputStream(), _refMap );
			RpcMetaMethodRequest methodResponse  = (RpcMetaMethodRequest) _rpcMetaResponse.read(tmis);
			methodRequest.setResponse( methodResponse );
		}
		catch (TypeException e1)
		{
			throw new IOException( e1.getMessage(), e1 );
		}
		catch (IOException e1)
		{
			throw e1;
		}
		finally
		{
			_back.closeLink(endPoint);
		}
	}



}
